﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYCodeBase.XmlUtility;
using HYFamilyLibDataManager;
using TableTools;
using TableTools.DataDefine;

namespace HYRElectric.MaterialStatistics
{
	public partial class MaterialsStatisticsForm : System.Windows.Forms.Form
	{
		public MaterialsStatisticsForm(ExternalCommandData cmdData, MaterialProgramInfos._Tag_TemplateType tab_TemplaType, bool isExcelTemp)
		{
			this.cmdData = cmdData;
			this.tab_TemplaType = tab_TemplaType;
			this.m_document = cmdData.Application.ActiveUIDocument.Document;
			this.InitializeComponent();
			this.isExcelTemlate = isExcelTemp;
		}

		private void MaterialsStatisticsForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			ToolTip toolTip = new ToolTip();
			toolTip.AutoPopDelay = 5000;
			toolTip.InitialDelay = 1000;
			toolTip.ReshowDelay = 50;
			toolTip.ShowAlways = true;
			toolTip.SetToolTip(this.btnAdd, "添加");
			toolTip.SetToolTip(this.btnEditor, "编辑");
			toolTip.SetToolTip(this.btnDelete, "删除");
			this.refreshList();
			if (this.dataGridView1.Rows.Count > 0 && this.dataGridView1.CurrentRow != null && this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[0].Value != null)
			{
				this.selectName = this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
				this.ProgramIdIndex = this.ProgramId();
			}
			this.rdoViewSet.Checked = true;
			this.InitRangeCombobox();
			this.LoadDlgData();
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			string text = "";
			if (this.dataGridView1.CurrentRow != null)
			{
				this.indexId = this.dataGridView1.CurrentRow.Index;
				TableInterface.AddMaterialTableProgram(this.cmdData, SubjectType.Electric, this.tab_TemplaType, ref text);
				this.refreshList();
				if (this.indexId < 0)
				{
					this.indexId++;
				}
				if (this.indexId > this.dataGridView1.Rows.Count - 1)
				{
					this.indexId--;
				}
				if (this.dataGridView1.RowCount > 1)
				{
					this.dataGridView1.CurrentCell = this.dataGridView1.Rows[this.dataGridView1.RowCount - 2].Cells[0];
				}
			}
			this.refreshList();
		}

		public string ProgramId()
		{
			if (this.dataGridView1.CurrentRow != null)
			{
				return this.m_ProgramIdList[this.dataGridView1.CurrentCell.RowIndex];
			}
			return "";
		}

		private void btnEditor_Click(object sender, EventArgs e)
		{
			if (this.dataGridView1.CurrentRow != null && this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[0].Value != null)
			{
				int rowIndex = this.dataGridView1.CurrentCell.RowIndex;
				if (rowIndex < 0)
				{
					return;
				}
				TableInterface.EditMaterialTableProgram(this.cmdData, this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString(), this.ProgramId(), SubjectType.Electric, this.tab_TemplaType, ref this.selectName);
				this.refreshList();
				if (this.dataGridView1.RowCount > 1)
				{
					this.dataGridView1.CurrentCell = this.dataGridView1.Rows[rowIndex].Cells[0];
					return;
				}
			}
			else
			{
				MessageBox.Show("请选择有效的方案", "盈建科提示");
			}
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			if (MessageBox.Show("确实要删除当前方案？", "盈建科提示", MessageBoxButtons.YesNo) == DialogResult.No)
			{
				return;
			}
			if (this.dataGridView1.CurrentRow == null || this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[0].Value == null)
			{
				MessageBox.Show("请选择有效的方案", "盈建科提示");
				return;
			}
			this.indexId = this.dataGridView1.CurrentRow.Index;
			TableInterface.RemoveProgram(SubjectType.Electric, this.dataGridView1.Rows[this.dataGridView1.CurrentRow.Index].Cells[0].Value.ToString(), this.ProgramId(), this.cmdData);
			this.refreshList();
			if (this.indexId < 0)
			{
				this.indexId++;
			}
			if (this.indexId > this.dataGridView1.Rows.Count - 1)
			{
				this.indexId--;
			}
			if (this.dataGridView1.RowCount > 1 && this.indexId >= 1)
			{
				this.dataGridView1.CurrentCell = this.dataGridView1.Rows[this.indexId - 1].Cells[0];
				return;
			}
			this.dataGridView1.CurrentCell = this.dataGridView1.Rows[0].Cells[0];
		}

		private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex >= 0 && this.dataGridView1.Rows[e.RowIndex].Cells[0].Value != null)
			{
				this.selectName = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
				this.ProgramIdIndex = this.ProgramId();
			}
		}

		public void refreshList()
		{
			this.dataGridView1.Rows.Clear();
			this.m_ProgramIdList.Clear();
			List<MaterialProgramInfos.BasicInfo> programInfoList = TableInterface.GetProgramInfoList(SubjectType.Electric, this.isExcelTemlate, this.cmdData);
			for (int i = 0; i < programInfoList.Count; i++)
			{
				MaterialProgramInfos.BasicInfo basicInfo = programInfoList[i];
				this.dataGridView1.Rows.Add(new object[]
				{
					basicInfo.m_Name,
					basicInfo.m_Describe
				});
				this.m_ProgramIdList.Add(basicInfo.m_Id);
			}
		}

		private void InitRangeCombobox()
		{
			this.cmbLevelMax.Items.Add("无限制");
			this.cmbLevelMin.Items.Add("无限制");
			foreach (Element element in ElementFiltering.FindSpecialElements(this.m_document, typeof(Level), BuiltInCategory.OST_Levels))
			{
				Level level = element as Level;
				this.cmbLevelMax.Items.Add(level.Name);
				this.cmbLevelMin.Items.Add(level.Name);
			}
			if (this.cmbLevelMax.Items.Count > 0)
			{
				this.cmbLevelMax.SelectedIndex = 0;
			}
			if (this.cmbLevelMin.Items.Count > 0)
			{
				this.cmbLevelMin.SelectedIndex = 0;
			}
		}

		private void cmbLevelMax_SelectedIndexChanged(object sender, EventArgs e)
		{
			StatisticsRange.Max = AssistFunc.GetLevelByName(this.m_document, this.cmbLevelMin.SelectedItem.ToString());
		}

		private void cmbLevelMin_SelectedIndexChanged(object sender, EventArgs e)
		{
			StatisticsRange.Min = AssistFunc.GetLevelByName(this.m_document, this.cmbLevelMax.SelectedItem.ToString());
		}

		private void rdoViewSet_CheckedChanged(object sender, EventArgs e)
		{
			this.cmbLevelMin.Enabled = false;
			this.cmbLevelMax.Enabled = false;
		}

		private void rdoConditionSet_CheckedChanged(object sender, EventArgs e)
		{
			this.cmbLevelMin.Enabled = true;
			this.cmbLevelMax.Enabled = true;
		}

		private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
		{
		}

		private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
		{
		}

		private void SaveDlgData()
		{
			string workingLocation = Product.WorkingLocation;
			string xmlPath = Path.Combine(workingLocation, "MateralTablesForm.xml");
			HYCodeBase.XmlUtility.XmlHelper xmlHelper = new HYCodeBase.XmlUtility.XmlHelper(workingLocation, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			xmlHelper.SetValue("CurrentCell", this.dataGridView1.CurrentCell.RowIndex.ToString());
			xmlHelper.SetValue("rdoConditionSet", this.rdoConditionSet.Checked.ToString());
			xmlHelper.SetValue("cmbLevelMin", this.cmbLevelMin.SelectedIndex.ToString());
			xmlHelper.SetValue("cmbLevelMax", this.cmbLevelMax.SelectedIndex.ToString());
			xmlHelper.SaveXml(xmlPath);
		}

		private void LoadDlgData()
		{
			string text = Path.Combine(Product.WorkingLocation, "MateralTablesForm.xml");
			if (!File.Exists(text))
			{
				return;
			}
			HYCodeBase.XmlUtility.XmlHelper xmlHelper = new HYCodeBase.XmlUtility.XmlHelper(text, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			if (this.dataGridView1.Rows.Count > 0)
			{
				this.dataGridView1.Rows[0].Selected = false;
				int num = Convert.ToInt32(xmlHelper.GetValue("CurrentCell"));
				if (this.dataGridView1.Rows.Count > num && num >= 0)
				{
					this.dataGridView1.Rows[num].Selected = true;
					this.dataGridView1.CurrentCell = this.dataGridView1.Rows[num].Cells[0];
				}
				else
				{
					this.dataGridView1.Rows[0].Selected = true;
				}
			}
			if (xmlHelper.GetValue("rdoConditionSet") == "True")
			{
				this.rdoConditionSet.Checked = true;
			}
			else
			{
				this.rdoViewSet.Checked = true;
			}
			this.cmbLevelMin.SelectedIndex = Convert.ToInt32(xmlHelper.GetValue("cmbLevelMin"));
			this.cmbLevelMax.SelectedIndex = Convert.ToInt32(xmlHelper.GetValue("cmbLevelMax"));
		}

		private void MaterialsStatisticsForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void MaterialsStatisticsForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult != DialogResult.OK)
			{
				return;
			}
			if (this.dataGridView1.CurrentRow != null && this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[0].Value != null)
			{
				StatisticsRange.Min = AssistFunc.GetLevelByName(this.m_document, this.cmbLevelMin.SelectedItem.ToString());
				StatisticsRange.Max = AssistFunc.GetLevelByName(this.m_document, this.cmbLevelMax.SelectedItem.ToString());
				if (this.dataGridView1.Rows.Count > 0 && this.dataGridView1.CurrentRow != null && this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[0].Value != null)
				{
					this.selectName = this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
					this.ProgramIdIndex = this.ProgramId();
				}
				this.SaveDlgData();
				return;
			}
			MessageBox.Show("请选择有效的方案", "盈建科提示");
			e.Cancel = true;
		}

		private Document m_document;

		private ExternalCommandData cmdData;

		public string selectName = "";

		private int indexId;

		private MaterialProgramInfos._Tag_TemplateType tab_TemplaType;

		private bool isExcelTemlate = true;

		public List<string> m_ProgramIdList = new List<string>();

		public string ProgramIdIndex;

		private XmlDocument xmlDoc = new XmlDocument();
	}
}
